package com.sunelec.controller;

import com.sunelec.mapper.InverterMapper;
import com.sunelec.mapper.PowerMapper;
import com.sunelec.pojo.Inverter;
import com.sunelec.pojo.Power;
import com.sunelec.pojo.PowerList;
import com.sunelec.pojo.Users;
import com.sunelec.service.ShowInfoService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Calendar;
import java.util.List;

/**
 * @Author: 郑明利
 * @Description: 详情展示
 * @Date: Create in 18:48 2018/05/08
 * @Modified By:
 */
@Controller
public class ShowInfoController {

    @Autowired
    private ShowInfoService showInfoService;

    @Autowired
    private PowerMapper powerMapper;

    @Autowired
    private InverterMapper inverterMapper;

    @RequestMapping("/ShowPowerInfo")
    public String showPowerInfo(String powerId, Model model){
        Power power = showInfoService.getPowerInfo(powerId);
        Users users = showInfoService.getUserInfo(power.getOwn());
        model.addAttribute("powerInfo",power);
        model.addAttribute("user",users);

        //根据powerId得到一个电站运行情况（发电量，功率，收益等）
        PowerList powerList = this.getPowerInfoByPowerSN(power.getPowerSn());
        String[] powerSNArray = null;
        if(power.getPowerSn() !=null){
            //目前SN号仅对应逆变器，还未对气象站，电表进行统计，不需要额外判断
            powerSNArray = power.getPowerSn().split(",");
        }
        int CNY = (int)(power.getPowerAvgElec()*powerList.getAll_elec());
        model.addAttribute("CNY",CNY);
        model.addAttribute("powerList",powerList);
        //发电功率和发电量的曲线图（日、月、年）
        //当前年月日
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        int day = cal.get(Calendar.DATE);
        String month_date = year + "-" + month + "-" + day;
        String month_month = year + "-" + month + "-";

        //1、日
        int[] power_trend_day = this.getPower_trend_day(powerSNArray,month_date);

        int[] power_elec_day = this.getPower_elec_day(powerSNArray,month_date);

        //2、月
        int[] power_trend_mon = new int[31];
        for (int i=0;i<31;i++) {
            for (int j=0;j<powerSNArray.length;j++){
                String string = year + "-" + month + "-" + (i+1);
                int a = 0;
                try{
                    a = inverterMapper.selectCurrentPowerMonth(powerSNArray[j],string);
                }catch (Exception e){
                    a = 0;
                }
                power_trend_mon[i] += a;
            }
        }

        int[] power_elec_mon = new int[31];
        for (int i=0;i<31;i++) {
            for (int j=0;j<powerSNArray.length;j++){
                String string = year + "-" + month + "-" + (i+1);
                int a = 0;
                try{
                    a = inverterMapper.selectPowerElecMonth(powerSNArray[j],string);
                }catch (Exception e){
                    a = 0;
                }
                power_elec_mon[i] += a;
            }
        }

        //3、年
        int[] power_trend_year = new int[12];
        for(int i=0;i<12;i++){
            for(int j=0;j<powerSNArray.length;j++){
                String month_start = year + "-" + (i+1) + "-1";
                String month_end =  year + "-" + (i+1) + "-31";
                power_trend_year[i] += inverterMapper.selectMonthCurrentPower(powerSNArray[j],month_start,month_end);
            }
        }

        int[] power_elec_year = new int[12];
        for(int i=0;i<12;i++){
            for(int j=0;j<powerSNArray.length;j++){
                String month_start = year + "-" + (i+1) + "-1";
                String month_end =  year + "-" + (i+1) + "-31";
                power_elec_year[i] += inverterMapper.selectMonthElec(powerSNArray[j],month_start,month_end);
            }
        }
        JSONArray power_trendArray_day = JSONArray.fromObject(power_trend_day);
        JSONArray power_elecArray_day  = JSONArray.fromObject(power_elec_day);
        JSONArray power_trendArray_mon = JSONArray.fromObject(power_trend_mon);
        JSONArray power_elecArray_mon  = JSONArray.fromObject(power_elec_mon);
        JSONArray power_trendArray_year  = JSONArray.fromObject(power_trend_year);
        JSONArray power_elecArray_year  = JSONArray.fromObject(power_elec_year);
        model.addAttribute("power_trendArray_day",power_trendArray_day);
        model.addAttribute("power_elecArray_day",power_elecArray_day);
        model.addAttribute("power_trendArray_mon",power_trendArray_mon);
        model.addAttribute("power_elecArray_mon",power_elecArray_mon);
        model.addAttribute("power_trendArray_year",power_trendArray_year);
        model.addAttribute("power_elecArray_year",power_elecArray_year);
        return "essentialInformation";
    }

    //根据powerId获取该电站下的所有信息
    public PowerList getPowerInfoByPowerSN(String powerSN){
        PowerList powerList = new PowerList();
        String[] powerSNArray = null;
        if(powerSN !=null){
            powerSNArray = powerSN.split(",");
        }
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        int day = cal.get(Calendar.DATE);
        String month_date = year + "-" + month + "-" + day;
        int current_power = 0;
        for (int i=0;i<powerSNArray.length;i++){
            try{
                current_power += inverterMapper.selectCurrentPower(powerSNArray[i],month_date);
            }catch (Exception e) {
                current_power += 0;
            }
        }
        //日发电量
        float daily_elec = 0;
        for (int i=0;i<powerSNArray.length;i++){
            daily_elec += inverterMapper.selectDailyElec(powerSNArray[i],month_date);
        }
        //月发电量
        float month_elec = 0;
        //开始
        String month_start =  year + "-" + month + "-1";
        //结束
        String month_end =  year + "-" + month + "-31";
        for (int i=0;i<powerSNArray.length;i++){
            month_elec += inverterMapper.selectMonthElec(powerSNArray[i],month_start,month_end);
        }
        //年电量
        float year_elec = 0;
        for (int i=0;i<powerSNArray.length;i++){
            year_elec += inverterMapper.selectMonthElec(powerSNArray[i],year+"-01-01",year+"-12-31");
        }
        //累计发电量
        float all_elec =0;
        for (int i=0;i<powerSNArray.length;i++){
            all_elec += inverterMapper.selectAllElec(powerSNArray[i]);
        }
        powerList.setDaily_electricity(daily_elec);
        powerList.setCurrent_power(current_power);
        powerList.setMonth_elec(month_elec);
        powerList.setYear_elec(year_elec);
        powerList.setAll_elec(all_elec);
        powerList.setUpdate_date(month_date);
        return powerList;
    }

    public int[] getPower_trend_day(String[] powerSNArray,String month_date){
        int[] power_trend_day = new int[8];
        for (int i=0;i<8;i++) {
            for (int j=0;j<powerSNArray.length;j++){
                String time_info = ":00:00";
                int time_num1 = i*3;
                int time_num2 = (i+1)*3;
                String time_start = time_num1 + time_info;
                String time_end = time_num2 + time_info;
                power_trend_day[i] += inverterMapper.selectSumCurrentPower(powerSNArray[j],month_date,time_start,time_end);
            }
        }
        return power_trend_day;
    }

    public int[] getPower_elec_day(String[] powerSNArray,String month_date){
        int[] power_elec_day = new int[8];
        for (int i=0;i<8;i++) {
            for (int j=0;j<powerSNArray.length;j++){
                String time_info = ":00:00";
                int time_num1 = i*3;
                int time_num2 = (i+1)*3;
                String time_start = time_num1 + time_info;
                String time_end = time_num2 + time_info;
                power_elec_day[i] += inverterMapper.selectPowerElec(powerSNArray[j],month_date,time_start,time_end);
            }
        }
        return power_elec_day;
    }

}
